<!DOCTYPE html><html><head><title>Json学习小结-2</title><meta charset='utf-8'><link href='https://dn-maxiang.qbox.me/res-min/themes/marxico.css' rel='stylesheet'><style>
.note-content  {font-family: 'Helvetica Neue', Arial, 'Hiragino Sans GB', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, Song, sans-serif;}

</style></head><body><div id='preview-contents' class='note-content'>
                        
                    



<h1 id="json学习小结-2">Json学习小结-2</h1>

<p>哎哟 2017-02-15</p>

<hr>



<h3 id="本小结内容">本小结内容</h3>

<p>解析null、boolean和数字</p>

<p>JSON 是树状结构，只包含 6 种数据类型：</p>

<ul><li>null：表示为null</li>
<li>boolean：表示为true或者false</li>
<li>number：一般的浮点数表示方法</li>
<li>string: 表示为 “…”</li>
<li>array: 表示为 [ … ]</li>
<li>object: 表示为 { … }</li>
</ul>

<hr>

<p><strong>1.</strong>根据叶大的教程，我们要实现的 JSON 库，主要是完成 3 个需求：</p>

<ul><li>把 JSON 文本解析为一个树状数据结构（parse）。</li>
<li>提供接口访问该数据结构（access）。</li>
<li>把数据结构转换成 JSON 文本（stringify）。</li>
</ul>



<p><img longdesc="http://i.imgur.com/vht0SmK.png" alt="" title="" type="image/png" src="" class=""></p>

<p><strong>2.</strong>下载的leptjson源码只包含三个文件：</p>

<ul><li>leptjson.h：leptjson 的头文件，含有对外类型和 API 函数声明。</li>
<li>leptjson.c：leptjson 的实现文件，含有内部的类型声明和函数实现。此文件会编译成库。</li>
<li>test.c：测试程序测试，需要链接 leptjson 库。</li>
</ul>

<p>为了解析三个数据类型，就要用到解析函数lept_parse(),其声明如下：</p>

<pre class="prettyprint hljs-dark"><code class="hljs aspectj"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse</span><span class="hljs-params">(lept_value* v, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* json)</span></span>;<br><span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse_null</span><span class="hljs-params">(lept_context* c, lept_value* v)</span></span>;<br><span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse_true</span><span class="hljs-params">(lept_context* c, lept_value* v)</span></span>; <br><span class="hljs-keyword">static</span> <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse_false</span><span class="hljs-params">(lept_context* c, lept_value* v)</span></span>;<br></code></pre>

<p>传入的 JSON 文本是一个const char* 类型的 C 字符串（空结尾字符串），const类型的对象是不能被改动的，所以我们无法改动这个输入字符串。 <br>
具体定义：</p>



<pre class="prettyprint hljs-dark"><code class="language-C++ hljs">    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse</span><span class="hljs-params">(lept_value* v, <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span>* json)</span> <br>    </span>{<br>        lept_context c;<br>        <span class="hljs-keyword">int</span> ret;<br>        assert(v != <span class="hljs-literal">NULL</span>);<br>        c.json = json;<br>        v-&gt;type = LEPT_NULL;<br>        lept_parse_whitespace(&amp;c);<br>        <span class="hljs-keyword">if</span> ((ret = lept_parse_value(&amp;c, v)) == LEPT_PARSE_OK) <br>        {<br>            lept_parse_whitespace(&amp;c);<br>            <span class="hljs-keyword">if</span> (*c.json != <span class="hljs-string">'\0'</span>)<br>                ret = LEPT_PARSE_ROOT_NOT_SINGULAR;<br>        }<br>        <span class="hljs-keyword">return</span> ret;<br>    }<br></code></pre>

<p>根据leptjson.c文件中已经定义好的test_ parse_null（）函数，我们可以编写自己的true、false函数。</p>

<p>true函数：</p>

<pre class="prettyprint hljs-dark"><code class="language-C++ hljs">    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse_true</span><span class="hljs-params">(lept_context* c, lept_value* v)</span> <br>    </span>{<br>        EXPECT(c, <span class="hljs-string">'t'</span>);<br>        <span class="hljs-keyword">if</span> (c-&gt;json[<span class="hljs-number">0</span>] != <span class="hljs-string">'r'</span> || c-&gt;json[<span class="hljs-number">1</span>] != <span class="hljs-string">'u'</span> || c-&gt;json[<span class="hljs-number">2</span>] != <span class="hljs-string">'e'</span>)<br>            <span class="hljs-keyword">return</span> LEPT_PARSE_INVALID_VALUE;<br>        c-&gt;json += <span class="hljs-number">3</span>;<br>        v-&gt;type = LEPT_TRUE;<br>        <span class="hljs-keyword">return</span> LEPT_PARSE_OK;<br>    }<br></code></pre>

<p>false函数：</p>

<pre class="prettyprint hljs-dark"><code class="language-C++ hljs">    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">lept_parse_false</span><span class="hljs-params">(lept_context* c, lept_value* v)</span> <br>    </span>{<br>        EXPECT(c, <span class="hljs-string">'f'</span>);<br>        <span class="hljs-keyword">if</span> (c-&gt;json[<span class="hljs-number">0</span>] != <span class="hljs-string">'a'</span> || c-&gt;json[<span class="hljs-number">1</span>] != <span class="hljs-string">'l'</span> || <br>            c-&gt;json[<span class="hljs-number">2</span>] != <span class="hljs-string">'s'</span> || c-&gt;json[<span class="hljs-number">3</span>] != <span class="hljs-string">'e'</span>)<br>            <span class="hljs-keyword">return</span> LEPT_PARSE_INVALID_VALUE;<br>        c-&gt;json += <span class="hljs-number">4</span>;<br>        v-&gt;type = LEPT_FALSE;<br>        <span class="hljs-keyword">return</span> LEPT_PARSE_OK;<br>    }<br></code></pre>

<p>最后修改test.c中的test_parse()函数：</p>



<pre class="prettyprint hljs-dark"><code class="language-C++ hljs">    <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">test_parse</span><span class="hljs-params">()</span> <br>    </span>{<br>        test_parse_null();<br>        test_parse_true();<br>        test_parse_false();<br>    }<br></code></pre>

<p>最后运行结果：</p>

<p><img longdesc="http://i.imgur.com/YBltZwu.png" alt="" title="" type="image/png" src="" class=""></p></div></body></html>